package com.linln.admin.pcs.controller;


import com.alibaba.fastjson.JSON;
import com.linln.admin.config.IDCardEncryptor;
import com.linln.admin.system.service.AppService;
import com.linln.common.data.QuerySpec;
import com.linln.common.utils.DateUtils;
import com.linln.common.utils.HelperUtil;
import com.linln.common.utils.HttpServletUtil;
import com.linln.component.actionLog.annotation.EntityParam;
import com.linln.component.shiro.ShiroUtil;
import com.linln.modules.system.domain.*;
import com.linln.modules.system.repository.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 投诉管理
 */
@Controller
@RequestMapping("/pcs/complain")
public class ComplainController {

    @Autowired
    private AppService appService;
    @Autowired
    private PorterRepository porterRepository;
    @Autowired
    private PorterConHireLogRepository porterConHireLogRepository;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;
    @Autowired
    private PorterPermissionRepository porterPermissionRepository;

    @Autowired
    private PatientRepository patientRepository;
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private ComplainRepository complainRepository;

    /**
     * 投诉列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("pcs:complain:index")
    public ResponseEntity<?> index(Model model, @EntityParam Complain complain) {

        return appService.layTable(complainRepository, before -> {

                    // 添加模板变量
                    return model;

                },

                after -> {


                    QuerySpec querySpec = QuerySpec.matching();
                    querySpec.withMatcher("name", QuerySpec.LIKE)
                            .withMatcher("sn", QuerySpec.LIKE);
                    User user = ShiroUtil.getSubject();
                    if (user.getType()!=null&&user.getType().equals("2")) {
                        Long companyId = user.getCompanyId();
                        querySpec.withMatcherValue("companyId", QuerySpec.EQUAL,companyId.toString());
                    }
                    return QuerySpec.of(querySpec);
                },

                "/pcs/complain/index",

                (pageResult) -> {
                    // 在这里处理 pageResult
                    // 例如：修改每一条数据
                 //   List<Porter> list = (List<Porter>) pageResult.getContent();
                    pageResult.getContent().forEach(item -> {
                        // 对每一项数据进行处理
                        Complain data = (Complain) item;
                        data.setCreateTimeStr(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", data.getCreateTime()));
                        try {
                            data.setCardNo(IDCardEncryptor.aesTuoMinDecrypt(data.getCardNo()));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    });
                }

        );

    }

    //详情页面
    @GetMapping("/detail")
    @RequiresPermissions("pcs:complain:detail")
    public String detail(Model model) {
        Integer id = HttpServletUtil.getParameterInt("id");
        Complain complain =complainRepository.getById(Long.valueOf(id));
        try {
            complain.setCardNo(IDCardEncryptor.aesTuoMinDecrypt(complain.getCardNo()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        complain.setCreateTimeStr(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", complain.getCreateTime()));
        List<String> pics = (List<String>) JSON.parse(complain.getPics());
        model.addAttribute("pics",pics);
        model.addAttribute("complain",complain);
        String patient_name = patientRepository.getById(complain.getPatientId()).getName();
        model.addAttribute("patient_name",patient_name);
        return  "/pcs/complain/detail";
    }


}
